package com.cisystem.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.cisystem.common.core.domain.entity.SysDept;
import com.cisystem.common.core.domain.entity.SysUser;
import com.cisystem.common.utils.DateUtils;
import com.cisystem.common.utils.StringUtils;
import com.cisystem.system.domain.*;
import com.cisystem.system.mapper.*;
import com.cisystem.system.service.IMailSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MailSendServiceImpl implements IMailSendService {
    @Autowired
    SysMailAccountMapper sysMailAccountMapper;

    @Autowired
    SysMailTemplateMapper sysMailTemplateMapper;

    @Autowired
    GoodCatchMapper goodCatchMapper;

    @Autowired
    InnovationMapper innovationMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysDeptMapper sysDeptMapper;

    @Autowired
    private SysMailLogMapper sysMailLogMapper;


    @Override
    public Long sendMailByAddGoodCatch(GoodCatch goodCatch) {

        if (goodCatch == null) return 0L;

        List<String> toMails = new ArrayList<>();
        List<String> ccMails = new ArrayList<>();
        ccMails.add("vincent.wu@ovt.com");
        //获取 owner 人员信息
        SysUser ownerUser = sysUserMapper.selectUserByEmployeeId(goodCatch.getOwnerId());
        if (ownerUser == null || StrUtil.isEmpty(ownerUser.getEmail())) return 0L;
        toMails.add(ownerUser.getEmail());

        Map<String, Object> templateParams = MapUtil.<String, Object>builder()
                .put("owner", ownerUser.getUserName())
                .put("title", goodCatch.getTitle())
                .build();

        SysMailLog sysMailLog = new SysMailLog();
        sysMailLog.setUserId(ownerUser.getUserId());
        sysMailLog.setAccountId(ownerUser.getUserId());
        sysMailLog.setUserType(1L);

        return doSendMail(sysMailLog, toMails, ccMails, "GoodCatchAdd", templateParams);
    }

    @Override
    public Long sendMailByUpdateGoodCatch(GoodCatch goodCatch) {

        if (goodCatch == null) return 0L;

        List<String> toMails = new ArrayList<>();
        List<String> ccMails = new ArrayList<>();
        ccMails.add("vincent.wu@ovt.com");
        //获取 提出人员信息
        SysUser user = sysUserMapper.selectUserByEmployeeId(goodCatch.getEmployeeId());
        if (user == null || StrUtil.isEmpty(user.getEmail())) return 0L;

        toMails.add(user.getEmail());

        SysUser ownerUser = sysUserMapper.selectUserByEmployeeId(goodCatch.getOwnerId());
        if (ownerUser == null || StrUtil.isEmpty(ownerUser.getEmail())) return 0L;
        toMails.add(ownerUser.getEmail());


        Map<String, Object> templateParams = MapUtil.<String, Object>builder()
                .put("name", goodCatch.getEmployeeName())
                .put("owner", ownerUser.getUserName())
                .put("title", goodCatch.getTitle())
                .build();

        SysMailLog sysMailLog = new SysMailLog();
        sysMailLog.setUserId(user.getUserId());
        sysMailLog.setAccountId(user.getUserId());
        sysMailLog.setUserType(1L);

        return doSendMail(sysMailLog, toMails, ccMails, "GoodCatchUpdate", templateParams);
    }

    public Long sendMailByOverdueGoodCatch(GoodCatch goodCatch) {

        if (goodCatch == null) return 0L;

        List<String> toMails = new ArrayList<>();
        List<String> ccMails = new ArrayList<>();
        ccMails.add("vincent.wu@ovt.com");
        //获取 owner 人员信息
        SysUser ownerUser = sysUserMapper.selectUserByEmployeeId(goodCatch.getOwnerId());
        if (ownerUser == null || StrUtil.isEmpty(ownerUser.getEmail())) return 0L;

        toMails.add(ownerUser.getEmail());
        //获取 提出人员信息
        SysUser user = sysUserMapper.selectUserByEmployeeId(goodCatch.getEmployeeId());
        if (user == null || StrUtil.isEmpty(user.getEmail())) return 0L;
        toMails.add(user.getEmail());

        Map<String, Object> templateParams = MapUtil.<String, Object>builder()
                .put("owner", ownerUser.getUserName())
                .put("name", user.getUserName())
                .put("title", goodCatch.getTitle())
                .build();

        SysMailLog sysMailLog = new SysMailLog();
        sysMailLog.setUserId(user.getUserId());
        sysMailLog.setAccountId(user.getUserId());
        sysMailLog.setUserType(1L);

        return doSendMail(sysMailLog, toMails, ccMails, "GoodCatchOverdue", templateParams);
    }

    private Long doSendMail(SysMailLog sysMailLog, Collection<String> tos, Collection<String> ccs, String templateCode, Map<String, Object> templateParams) {
        if (CollUtil.isEmpty(tos)) return 0L;
        SysMailTemplate sysMailTemplate = sysMailTemplateMapper.selectSysMailTemplateByCode(templateCode);
        if (sysMailTemplate == null) {
            System.out.println("mail(" + templateCode + ") template not exist!");
            return 0L;
        }

        String title = StrUtil.format(sysMailTemplate.getTitle(), templateParams);
        String content = StrUtil.format(sysMailTemplate.getContent(), templateParams);

        sysMailLog.setTemplateId(sysMailTemplate.getId());

        sysMailLog.setTemplateCode(templateCode);
        sysMailLog.setTemplateTitle(sysMailTemplate.getTitle());
        sysMailLog.setTemplateContent(content);
        sysMailLog.setTemplateParams(MapUtil.join(templateParams, ",", ":", false));

        // 获取所有邮件配置信息
        List<SysMailAccount> sysMailAccounts = sysMailAccountMapper.selectSysMailAccountList(null);
        sysMailLog.setToMail(ArrayUtil.join(tos.toArray(), ","));
        for (SysMailAccount sysMailAccount : sysMailAccounts) {
            sysMailLog.setFromMail(sysMailAccount.getMail());
            MailAccount mailAccount = new MailAccount().setFrom(sysMailAccount.getMail()).setAuth(true)
                    .setUser(sysMailAccount.getUsername()).setPass(sysMailAccount.getPassword())
                    .setHost(sysMailAccount.getHost()).setPort(sysMailAccount.getPort().intValue());
            String msg = MailUtil.send(mailAccount, tos, ccs, null, title, content, true);
            sysMailLog.setSendTime(DateUtils.getNowDate());
            sysMailLog.setSendStatus(1L);
            sysMailLog.setRemark(msg);
            sysMailLogMapper.insertSysMailLog(sysMailLog);
        }
        return 1L;
    }

    @Override
    public Long sendMailByAddInnovation(Innovation innovation) {

        if (innovation == null) return 0L;

        List<String> toMails = new ArrayList<>();
        List<String> ccMails = new ArrayList<>();
        ccMails.add("vincent.wu@ovt.com");
        //获取 manager 人员信息
        SysUser sysUser = sysUserMapper.selectUserByEmployeeId(innovation.getEmployeeId());
        if (sysUser == null || StrUtil.isEmpty(sysUser.getEmail())) return 0L;

        SysDept sysDept = sysDeptMapper.selectDeptById(sysUser.getDeptId());
        if (sysDept == null || StrUtil.isEmpty(sysDept.getEmail())) return 0L;

        toMails.add(sysDept.getEmail());
        Map<String, Object> templateParams = MapUtil.<String, Object>builder()
                .put("manager", sysDept.getLeader())
                .put("title", innovation.getTitle())
                .build();

        SysMailLog sysMailLog = new SysMailLog();
        sysMailLog.setUserId(sysUser.getUserId());
        sysMailLog.setAccountId(sysUser.getUserId());
        sysMailLog.setUserType(1L);

        return doSendMail(sysMailLog, toMails, ccMails, "InnovationAdd", templateParams);
    }

    @Override
    public Long sendMailByUpdateInnovation(Innovation innovation) {

        if (innovation == null) return 0L;

        List<String> toMails = new ArrayList<>();
        List<String> ccMails = new ArrayList<>();
        ccMails.add("vincent.wu@ovt.com");
        //获取 提出人员信息
        SysUser user = sysUserMapper.selectUserByEmployeeId(innovation.getEmployeeId());
        if (user == null || StrUtil.isEmpty(user.getEmail())) return 0L;
        toMails.add(user.getEmail());

        Map<String, Object> templateParams = MapUtil.<String, Object>builder()
                .put("name", innovation.getEmployeeName())
                .put("remark", innovation.getRemark())
                .build();

        SysMailLog sysMailLog = new SysMailLog();
        sysMailLog.setUserId(user.getUserId());
        sysMailLog.setAccountId(user.getUserId());
        sysMailLog.setUserType(1L);

        return doSendMail(sysMailLog, toMails, ccMails, "InnovationUpdate", templateParams);
    }


    public Long sendMailByOverdueInnovation(Innovation innovation) {

        if (innovation == null) return 0L;

        List<String> toMails = new ArrayList<>();
        List<String> ccMails = new ArrayList<>();
        ccMails.add("vincent.wu@ovt.com");

        //获取 提出人员信息
        SysUser user = sysUserMapper.selectUserByEmployeeId(innovation.getEmployeeId());
        if (user == null || StrUtil.isEmpty(user.getEmail())) return 0L;
        toMails.add(user.getEmail());

        SysDept sysDept = sysDeptMapper.selectDeptById(user.getDeptId());
        if (sysDept == null || StrUtil.isEmpty(sysDept.getEmail())) return 0L;

        toMails.add(sysDept.getEmail());
        Map<String, Object> templateParams = MapUtil.<String, Object>builder()
                .put("manager", sysDept.getLeader())
                .put("name", user.getUserName())
                .put("title", innovation.getTitle())
                .build();

        SysMailLog sysMailLog = new SysMailLog();
        sysMailLog.setUserId(user.getUserId());
        sysMailLog.setAccountId(user.getUserId());
        sysMailLog.setUserType(1L);

        return doSendMail(sysMailLog, toMails, ccMails, "InnovationOverdue", templateParams);
    }

}
